/*
 * Copyright (c) Created by T C L on 2019.7.8.
 * 会员优惠券
 */

package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.SuperscriptSpan;
import android.view.View;
import android.widget.TextView;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.TicketEntity;


public class ADA_MemberCouponList extends CommonAdapter<TicketEntity> {

    public ADA_MemberCouponList(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_member_coupon;
    }

    @Override
    protected void convert(ViewHolder holder, final TicketEntity item, final int position) {

        // 设置基本数据
        if (item.getStatus().equals("0")) {
            holder.setBackgroundRes(R.id.ticket_bg_layout, R.mipmap.ic_member_coupon_header_unused);
            holder.setVisible(R.id.tv_expired, false);
        }
        if (item.getStatus().equals("2")) {
            holder.setBackgroundRes(R.id.ticket_bg_layout, R.mipmap.ic_member_coupon_header_expired);
            holder.setVisible(R.id.tv_expired, true);
        }

        holder.setText(R.id.tv_ticket_name, item.getTicketName());

        String couponAvailableTime = AppDateUtil.getTimeStamp(item.getStartDate(), AppDateUtil.YYYY_MM_DD2) +
                " - " +
                AppDateUtil.getTimeStamp(item.getEndDate(), AppDateUtil.YYYY_MM_DD2);
        holder.setText(R.id.tv_ticket_last_time, String.format("有效期：%s", couponAvailableTime));

        TextView tvUtility = holder.getView(R.id.tv_coupon_utility);

        // 根据优惠券类型设置条目样式展示
        switch (item.getTicketType()) {
            case TicketEntity.TICKET_TYPE_DISCOUNT:
                holder.setText(R.id.tv_ticket_amount, BigDecimalUtil.getNumber(item.getTicketAmount()));
                String condition = "[满" + BigDecimalUtil.getNumber(item.getLimitAmount()) + "元可用]";
                holder.setVisible(R.id.tv_coupon_condition, true);
                holder.setText(R.id.tv_coupon_condition, condition);
                holder.setText(R.id.tv_ticket_type, "满减券");
                tvUtility.setVisibility(View.INVISIBLE);
                break;
            case TicketEntity.TICKET_TYPE_EXPERIMENT:
                holder.setText(R.id.tv_ticket_amount, BigDecimalUtil.getNumber(item.getTicketAmount()));
                ((TextView) holder.getView(R.id.tv_ticket_amount)).setTextSize(40);
                holder.setText(R.id.tv_ticket_type, "体验券");
//                String utility = getCouponUtility(item);
//                tvUtility.setText(utility);
                tvUtility.setText(item.getServiceName());
                holder.setVisible(R.id.tv_coupon_condition, false);
                tvUtility.setVisibility(View.VISIBLE);
                break;
            case TicketEntity.TICKET_TYPE_COUPON:
                holder.setText(R.id.tv_ticket_type, "折扣券");
                SpannableString spannableString = new SpannableString(item.getTicketDiscount() + "折");
                spannableString.setSpan(new SuperscriptSpan(), spannableString.length() - 1, spannableString.length()
                        , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.setText(R.id.tv_ticket_amount, item.getTicketDiscount() + "折");
                ((TextView) holder.getView(R.id.tv_ticket_amount)).setTextSize(16);
                holder.setVisible(R.id.tv_coupon_condition, false);
                tvUtility.setVisibility(View.INVISIBLE);
                break;
        }
    }

    private String getCouponUtility(TicketEntity item) {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < (item.getServiceList() == null ? 0 : item.getServiceList().size()); i++) {
            buffer.append(item.getServiceList().get(i).getServiceName()).append(" ");
        }
        return buffer.toString().trim();
    }
}
